//
//  WBNetworkTool.m
//  WeiBoOC
//
//  Created by 韩菲菲 on 16/8/11.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

#import "WBNetworkTool.h"
#import "WBUserAccount.h"
#import "WBUserAccountViewModel.h"

@implementation WBNetworkTool

//单例
static WBNetworkTool* _instance;
+ (instancetype)sharedWBNetworkTool
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[WBNetworkTool alloc] init];
        
        _instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/plain", nil];
        
    });
    
    return _instance;
}


- (void)requestWithWBNetworkToolMethod:(WBNetworkToolMethod)method andURLString:(NSString *)urlString andParameters:(id)parameters andSuccess:(void(^)(id responseObject))success andFailure:(void(^)(NSError *error))failure
{
    if (method == GET) {
        //GET请求
        [self GET:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            //数据请求成功
            success(responseObject);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            failure(error);
            
        }];
    }else
    {
        //POST请求
        [self POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            //数据请求成功
            success(responseObject);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            failure(error);
            
        }];

    }

}

#pragma mark - 根据code请求token
- (void)getUserTokenWithCode:(NSString *)code andSuccess:(void (^)(id))success andFailure:(void (^)(NSError *))failure
{
    NSString *urlString = @"https://api.weibo.com/oauth2/access_token";
    NSDictionary *paras = @{
                            
                            @"client_id":WBAppKey,
                            @"client_secret":WBAppSecret,
                            @"grant_type":@"authorization_code",
                            @"code":code,
                            @"redirect_uri":WBRedirect_uri
                            
                            };
    
    [self requestWithWBNetworkToolMethod:POST andURLString:urlString andParameters:paras andSuccess:success andFailure:failure];

}


//根据token去请求个人信息
#pragma mark - 根据token请求个人信息
- (void)getUserInfoWithUserAccessToken:(WBUserAccount *)userAccount andSuccess:(void(^)(id responseObject))success andFailure:(void(^)(NSError *error))failure
{
    
    NSString *urlString = @"https://api.weibo.com/2/users/show.json";
    
    //参数
    NSDictionary *paras = @{
                            @"access_token":userAccount.access_token,
                            @"uid":userAccount.uid
                            };
    [self requestWithWBNetworkToolMethod:GET andURLString:urlString andParameters:paras andSuccess:success andFailure:failure];

}

#pragma mark - 获取首页数据
- (void)getHomeDataWithMaxId:(int64_t)maxId andSinceId:(int64_t)sinceId andSuccess:(void(^)(id responseObject))success andFailure:(void(^)(NSError *error))failure
{
    //设置urlstring
    NSString *urlString = @"https://api.weibo.com/2/statuses/home_timeline.json";
    
    //设置上拉加载更多 下拉刷新参数
    //设置参数
    NSDictionary *dict = @{
                           @"access_token":[WBUserAccountViewModel sharedWBUserAccountViewModel].accessToken,
                           @"since_id":[NSString stringWithFormat:@"%lld",sinceId],
                           @"max_id":[NSString stringWithFormat:@"%lld",maxId]
                           };
    
    [self requestWithWBNetworkToolMethod:GET andURLString:urlString andParameters:dict andSuccess:success andFailure:failure];


}

#pragma mark - 发布微博数据
- (void)updateSinaWithStatus:(NSString *)status andSuccess:(void(^)(id responseObject))success andFailure:(void(^)(NSError *error))failure
{
    NSString *urlString = @"https://api.weibo.com/2/statuses/update.json";
    
    NSDictionary *dict = @{
                           @"access_token":[WBUserAccountViewModel sharedWBUserAccountViewModel].accessToken,
                           @"status":status
                           };
    
    [self requestWithWBNetworkToolMethod:POST andURLString:urlString andParameters:dict andSuccess:success andFailure:failure];

}

- (void)uploadSinaWithStatus:(NSString *)status andImage:(UIImage *)image andSuccess:(void(^)(id responseObject))success andFailure:(void(^)(NSError *error))failure
{
    NSString *urlString = @"https://upload.api.weibo.com/2/statuses/upload.json";
    
    NSDictionary *dict = @{
                           @"source":WBAppKey,
                           @"access_token":[WBUserAccountViewModel sharedWBUserAccountViewModel].accessToken,
                           @"status":status
                           };
    
    [self POST:urlString parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSData *data = UIImagePNGRepresentation(image);
       [formData appendPartWithFileData:data name:@"pic" fileName:@"xxx" mimeType:@"application/octet-stream"];
        
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        success(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(error);
        
    }];
    
}

@end
